package cate.game.role.task;


import cate.common.table.d.GDFunc;
import cate.common.table.d.GDHistory;
import cate.game.role.Role;
import cate.game.role.bag.equip.Equip;
import cate.game.role.bag.gem.Gem;
import cate.game.role.bag.hero.Hero;
import cate.game.role.fight.HeroGrid;
import cate.game.role.fight.Position;
import lombok.extern.slf4j.Slf4j;

/**
 * 任务初始化时候的初始值设置
 */
@Slf4j
public class TaskInitValue {
    private TaskInitValue(){}

    public static void handle(TaskItem task) {
        try {
            switch (task.getTpl().event) {
                case GDHistory.Event.LOGIN_DAYS:
                    if (task.value <= 0) {
                        task.value = 1;
                    }
                    break;
                case GDHistory.Event.ROLE_LEVEL:
                    task.value = task.getRole().getBase().level;
                    break;
                case GDHistory.Event.VIP_LEVEL:
                    task.value = task.getRole().getBase().vip;
                    break;
                case GDHistory.Event.ROLE_GRADE_UP:
                    task.value = task.getRole().getBase().grade;
                    break;
                case GDHistory.Event.FIGHT_POWER:
                    task.value = task.getRole().getBase().powerMax;
                    break;
                case GDHistory.Event.POSITION_SET_NUM:
                    task.value = task.getRole().getFight().maxFuncPositionNum();
                    break;
                case GDHistory.Event.HERO_NOW_LEVEL_NUM:
                    task.value = heroLevelNum(task.getRole(), task.getTpl().param);
                    break;
                case GDHistory.Event.SPICE_GET:
                    task.value = task.getRole().getBag().spice.getList().size();
                    break;
                case GDHistory.Event.GEM_LEVEL_NUM:
                    task.value = gemLevelNum(task.getRole(), task.getTpl().param);
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("大概率是因为找不到tid={}的任务", task.tid);
        }
    }

    private static int heroLevelNum(Role role, int level) {
        int num = 0;
        for (Hero hero : role.getBag().hero.getList()) {
            if (hero.level >= level) {
                num++;
            }
        }
        return num;
    }

    private static int gemLevelNum(Role role, int level) {
        int num = 0;
        Position position = role.getFight().getPosition(GDFunc.ADVENTURE);
        if (position != null) {
            for (HeroGrid grid : position.grids) {
                Hero hero = role.getBag().hero.getItem(grid.uid);
                if (hero != null && hero.equipsOn != null && !hero.equipsOn.equipMap.isEmpty()) {
                    for (Equip equip : hero.equipsOn.equipMap.values()) {
                        if (equip.hasGem()) {
                            for (Gem gem : equip.getGemsOn().getPosItems().values()) {
                                if (gem.getTpl(role.getGame()).level >= level) {
                                    num ++;
                                }
                            }
                        }
                    }
                }
            }
        }
        return num;
    }
}
